package cn.shuangbofu.concurrent;

import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by shuangbofu on 2020/7/24 11:29
 */
public class Test {
    public static void main(String[] args) {

        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        Request request = new Request();
        CompletableFuture<Request> future = CompletableFuture.completedFuture(request);

//        future.thenApplyAsync(Test::handleRequest, threadPool)
//                .thenApplyAsync();

        ArrayList<Message> messages = Lists.newArrayList(new Message());
        for (Message message : messages) {
            handleRead0(message);
        }
    }

    public static void handleRead0(Message message) {


    }

    public static Response<String> handleRequest(Request<HeartBeatRequest> request) {
        return null;
    }

    public static Type getTypeByClass(Class<? extends MessageLite> clazz) {
        Map<Class<?>, Type> map = new HashMap<>();
        map.put(HeartBeatRequest.class, Type.heartbeat);


        return map.get(clazz);
    }

    public static <T extends MessageLite> T parse2Entity(Class<T> clazz, ByteString data) {
        Type type = getTypeByClass(clazz);
        if (type != null) {
            switch (type) {
                case response:
//                return
                    return null;
                case heartbeat:
//                    return HeartBeatRequest.parseFrom(data);
                    return null;
                default:
                    return null;
            }
        }
        return null;
    }

    static enum Type {
        /**
         *
         */
        heartbeat, response
    }

    static interface RequestHandler<T extends MessageLite> {
        void onReceive(Request<T> request);
    }

    static interface ResponseHandler<B> {
        void onReceiveResponse(Response<B> response);
    }

    static interface MessageLite {
        ByteString toByteString();
    }

    static class Message implements MessageLite {
        Type type;
        ByteString data;

        @Override
        public ByteString toByteString() {
            return null;
        }
    }

    static class Res implements MessageLite {
        Type reqType;
        ByteString data;

        @Override
        public ByteString toByteString() {
            return null;
        }
    }

    static class HeartBeatRequest implements MessageLite {

        public static HeartBeatRequest parseFrom(ByteString byteString) {
            return new HeartBeatRequest();
        }

        @Override
        public ByteString toByteString() {
            return null;
        }
    }

    static class Request<T extends MessageLite> {
        T data;
    }

    static class Response<T> {
        T data;
    }
}
